package com.home.base.core.context;

import com.home.base.core.constants.UserInfoConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 后台当前上下文
 * Created by 王大宸 on 2020-09-23 1:18
 * Created with IntelliJ IDEA.
 */
public class BaseContextHandler {

    /**
     * 获取用户id
     */
    public static String getUserId() {
        Object value = get(UserInfoConstant.USER_ID);
        return returnObjectValue(value);
    }

    public static void setUserId(String userId) {
        set(UserInfoConstant.USER_ID, userId);
    }

    /**
     * 获取登录账号
     */
    public static String getAccount() {
        Object value = get(UserInfoConstant.USER_ACCOUNT);
        return returnObjectValue(value);
    }

    public static void setAccount(String username) {
        set(UserInfoConstant.USER_ACCOUNT, username);
    }

    /**
     * 获取用户姓名
     */
    public static String getName() {
        Object value = get(UserInfoConstant.USER_NAME);
        return returnObjectValue(value);
    }

    public static void setName(String name) {
        set(UserInfoConstant.USER_NAME, name);
    }

    /**
     * 获取头像
     */
    public static String getAvatar() {
        Object value = get(UserInfoConstant.AVATAR);
        return returnObjectValue(value);
    }

    public static void setAvatar(String avatar) {
        set(UserInfoConstant.AVATAR, avatar);
    }

    /**
     * 获取部门id
     */
    public static String getDepartId() {
        Object value = get(UserInfoConstant.DEPART_ID);
        return returnObjectValue(value);
    }

    public static void setDepartId(String depart) {
        set(UserInfoConstant.DEPART_ID, depart);
    }

    /**
     * 获取用户类型
     */
    public static String getUserType() {
        Object value = get(UserInfoConstant.USER_TYPE);
        return returnObjectValue(value);
    }

    public static void setUserType(String type) {
        set(UserInfoConstant.USER_TYPE, type);
    }

    /**
     * 获取用户微信openId
     */
    public static String getOpenId() {
        Object value = get(UserInfoConstant.OPEN_ID);
        return returnObjectValue(value);
    }

    public static void setOpenId(String openId) {
        set(UserInfoConstant.OPEN_ID, openId);
    }

    /**
     * 获取用户微信unionId
     */
    public static String getUnionId() {
        Object value = get(UserInfoConstant.UNION_ID);
        return returnObjectValue(value);
    }

    public static void setUnionId(String unionId) {
        set(UserInfoConstant.UNION_ID, unionId);
    }

    /**
     * 获取用户联系电话
     */
    public static String getTelPhone() {
        Object value = get(UserInfoConstant.TEL_PHONE);
        return returnObjectValue(value);
    }

    public static void setTelPhone(String telPhone) {
        set(UserInfoConstant.TEL_PHONE, telPhone);
    }

    /**
     * 获取用户性别
     */
    public static String getSex() {
        Object value = get(UserInfoConstant.SEX);
        return returnObjectValue(value);
    }

    public static void setSex(String sex) {
        set(UserInfoConstant.SEX, sex);
    }

    /**
     * 获取用户电子邮箱
     */
    public static String getEmail() {
        Object value = get(UserInfoConstant.EMAIL);
        return returnObjectValue(value);
    }

    public static void setEmail(String email) {
        set(UserInfoConstant.EMAIL, email);
    }


    /**
     * 切勿修改下面的方法
     */
    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (null == map) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (null == map) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static Object getUserInfo() {
        Map<String, Object> map = threadLocal.get();
        return map;
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        threadLocal.remove();
    }
}
